package com.wan37.logic.skill.impl;

import com.wan37.logic.component.Worker;
import com.wan37.logic.domain.vo.AllRoleVO;
import com.wan37.logic.skill.AbstractEnemyAOESkill;
import com.wan37.protobuf.constant.*;
import com.wan37.logic.domain.vo.MonsterVO;
import com.wan37.logic.domain.vo.RoleVO;
import com.wan37.logic.handler.WorkerHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 中毒类aoe技能，在duration内每秒造成TRUE_DAMAGE伤害
 *
 * @author : luoyong
 * @date : 2020-06-28 16:38
 **/
@Component(SkillLabel.AOE + SkillLabel.ENEMY + SkillLabel.POISONING)
@Scope("prototype")
public class PoisoningSkillEnemy extends AbstractEnemyAOESkill {
    private final static Logger logger = LoggerFactory.getLogger(PoisoningSkillEnemy.class);

    @Override
    public Map<String, Map<String, Long>> handle(String sceneId) {
        for (int i = 0; i < skillVO.getDuration(); i++) {
            delayDequeThreadPollUtil.updateTimeSchedule(i, allRoleVO.getAllRoleId(), sceneId, new Fire(sceneId));
        }
        return new HashMap<>();
    }

    class Fire implements WorkerHandler {
        private String oldSceneId;
        private String newSceneId;

        public Fire(String oldSceneId) {
            this.oldSceneId = oldSceneId;
        }

        @Override
        public void execute(Worker worker) throws Exception {
            Map<String, Map<String, Long>> allRoleVONumberMap = new HashMap<>();
            List<String> allRoleIdList = new ArrayList<>();
            int code = CommonFighting.ATTACK_SUCCESS_DELAY_RESULT;
            for (AllRoleVO allRoleVO2 : allRoleVOList) {
                if (allRoleVO2.getLabel() == AllRoleLabel.MONSTER) {
                    newSceneId = ((MonsterVO) allRoleVO2).getSceneId();
                } else if (allRoleVO2.getLabel() == AllRoleLabel.ROLE) {
                    newSceneId = userServiceCache.getUserServiceMap().get(((RoleVO) allRoleVO2).getUserId()).getSceneId();
                }
                if (oldSceneId.equals(newSceneId)) {
                    if (allRoleVO2.getStatus() != AllRoleLabel.STATUS_DIE) {
                        long mp = allRoleVO2.getNumberMap().get(CommonBase.MP) - skillVO.getNumberMap().get(CommonBase.TRUE_DAMAGE);
                        Map<String, Long> numberMap = new HashMap<>();
                        if (mp < 0) {
                            allRoleVO2.getNumberMap().put(CommonBase.MP, (long) 0);
                            allRoleVO2.setStatus(AllRoleLabel.STATUS_DIE);
                            numberMap.put(CommonBase.MP, (long) 0);
                            allRoleVONumberMap.put(allRoleVO2.getAllRoleId(), numberMap);
                            allRoleIdList.add(allRoleVO2.getAllRoleId());
                        } else {
                            allRoleVO2.getNumberMap().put(CommonBase.MP, mp);
                            numberMap.put(CommonBase.MP, mp);
                            allRoleVONumberMap.put(allRoleVO2.getAllRoleId(), numberMap);
                            allRoleIdList.add(allRoleVO2.getAllRoleId());
                        }
                        logger.info("发生一次持久性伤害，{} 受到攻击伤害值：{}", allRoleVO2.getName(), skillVO.getNumberMap().get(CommonBase.TRUE_DAMAGE));
                    }
                }
            }
            if (oldSceneId.equals(newSceneId)) {
                if(sceneServiceCache.getSceneServiceMap().containsKey(oldSceneId)){
                    routeUtils.sendAttackBroadcastResponse(code, sceneServiceCache.getSceneServiceMap()
                            .get(oldSceneId).getUserIdSet(), allRoleVO.getAllRoleId(), allRoleIdList, skillVO.getId(), allRoleVONumberMap);
                }
            }
        }
    }
}
